package com.itcanteen.medical.patient.util;

import org.I0Itec.zkclient.ZkClient;

public abstract class AbstractLock {

    //连接zookeeper地址
    public static final String ZK_ADDR = "127.0.0.1:2181";
    //连接超时时间
    public static final int SESSION_TIMEOUT = 10000;
    //创建客户端开始连接
    protected ZkClient zkClient = new ZkClient(ZK_ADDR,SESSION_TIMEOUT);

    //进行加锁
    public void getLock(String lock){
       //模拟人的名称
        String threadName = Thread.currentThread().getName();


        if (tryLock(lock)){
            System.out.println(threadName+"--加锁成功");
        }else {
            System.out.println(threadName+"--加锁失败，等待加锁");
            //进行等待
            waitLock();
            //重新加锁
            getLock(lock);

        }

    }

    //释放锁
    public abstract void releaseLock();

    //加锁
    public abstract boolean tryLock(String lock);

    //等待加锁
    public abstract void waitLock();

}
